package com.yidu.t299.dao.twj;

import com.yidu.t299.domain.twj.SysRole;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface SysRoleMapper {
    /**
     * 新增角色相关权限信息
     */
    @Insert("<script> " +
            "insert into sys_role_right(rf_role_id, rf_right_code)\n" +
            "     <foreach collection=\"rightCodeList\" open=\" values\" separator=\",\" item=\"item\">\n" +
            "         (#{roleId}, #{item})\n" +
            "     </foreach>" +
            "</script> ")
    void insertRoleRight(SysRole sysRole);

    /**
     * 根据角色删除相关权限信息
     */
    @Delete("delete from sys_role_right where rf_role_id = #{rfRoleId}")
    void deleteRoleRight(Integer rfRoleId);

    /**
     * 根据主键删除角色信息(实际上是修改flag字段的值，不能直接删除信息)
     * @param roleId 角色编号
     * @return 影响的行数(1:成功)
     */
    @Delete("update sys_role set role_flag=0 where role_id = #{roleId}")
    int deleteByPrimaryKey(Integer roleId);

    /**
     * 根据主键批量删除角色信息(实际上是修改flag字段的值，不能直接删除信息)
     * @param array 角色编号数组
     * @return 影响的行数(1:成功)
     */
    @Delete("<script> " +
            "        update sys_role set role_flag=0 where role_id in " +
            "        <foreach collection=\"array\" open=\"(\" close=\")\" separator=\",\" item=\"roleId\"> " +
            "            #{roleId,jdbcType=INTEGER} " +
            "        </foreach> " +
            "</script>")
    int deleteBatch(Integer[] array);

    /**
     * 添加角色信息
     * @param sysRole 角色对象
     */
    @Insert("insert into sys_role (role_id, role_name, role_desc, role_flag)\n" +
            "    values (#{roleId,jdbcType=INTEGER}, #{roleName,jdbcType=VARCHAR}, #{roleDesc,jdbcType=VARCHAR}, \n" +
            "      #{roleFlag,jdbcType=INTEGER}) ")
    void insert(SysRole sysRole);

    /**
     * 根据主键查询角色信息
     * @param roleId 角色编号
     * @return 角色对象
     */
    @Select("SELECT * from sys_role " +
            "WHERE role_id = #{roleId} AND role_flag=1 ")
    @Results(id = "BaseResultMap",value = {
            @Result(id = true,column = "role_id",property = "roleId"),
            @Result(column = "role_name",property = "roleName"),
            @Result(column = "role_desc",property = "roleDesc"),
            @Result(column = "role_flag",property = "roleFlag"),
            @Result(column = "role_id",property = "rightCodeList",
            one = @One(select = "com.yidu.t299.dao.twj.SysRoleMapper.selectRightCodeList"))
    })
    SysRole selectByPrimaryKey(Integer roleId);

    /**
     * 根据主键查询角色信息(不筛选状态)
     * @param roleId 角色编号
     * @return 角色对象
     */
    @Select("SELECT * from sys_role " +
            "WHERE role_id = #{roleId} ")
    @ResultMap("BaseResultMap")
    SysRole selectById(Integer roleId);

    /**
     * 根据角色编号查询所有权限信息
     * @param roleId 角色编号
     * @return 权限列表
     */
    @Select("select rf_right_code from sys_role_right where rf_role_id=#{roleId}")
    List<String> selectRightCodeList(Integer roleId);


    /**
     * 根据所给属性值来修改其字段值
     * @param sysRole 角色对象
     */
    @Update("<script> " +
            "update sys_role\n" +
            "    <set>\n" +
            "      <if test=\"roleName != null\">\n" +
            "        role_name = #{roleName,jdbcType=VARCHAR},\n" +
            "      </if>\n" +
            "      <if test=\"roleDesc != null\">\n" +
            "        role_desc = #{roleDesc,jdbcType=VARCHAR},\n" +
            "      </if>\n" +
            "      <if test=\"roleFlag != null\">\n" +
            "        role_flag = #{roleFlag,jdbcType=INTEGER},\n" +
            "      </if>\n" +
            "    </set>\n" +
            "    where role_id = #{roleId,jdbcType=INTEGER} " +
            "</script> ")
    void update(SysRole sysRole);


    /**
     * 批量查询
     * @param sysRole 角色对象
     * @return 角色对象集合
     */
    @Select("<script> " +
            "select role_id, role_name, role_desc,role_flag from sys_role\n" +
            "        <where>\n" +
            "            <if test=\"roleName != null and roleName != ''\">\n" +
            "                role_name like concat('%',#{roleName},'%')\n" +
            "            </if>\n" +
            "        </where> " +
            "</script> ")
    @ResultMap("BaseResultMap")
    List<SysRole> select(SysRole sysRole);

}